package com.yanq.liancar.view;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/6  10:06
 *  @描述       ${TODO}
 */


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.yanq.liancar.R;

public class StaffSideLetterBar extends View {
    private static final String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#"};

    private int choose = -1;

    private Paint paint = new Paint();

    private boolean showBg = false;

    private OnLetterChangedListener onLetterChangedListener;

    private TextView overlay;



    public StaffSideLetterBar(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }



    public StaffSideLetterBar(Context context, AttributeSet attrs) {

        super(context, attrs);

    }



    public StaffSideLetterBar(Context context) {

        super(context);

    }



    /**

     * 设置悬浮的textview

     * @param overlay

     */

    public void setOverlay(TextView overlay){

        this.overlay = overlay;

    }



    @SuppressWarnings("deprecation")

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        if (showBg) {

            canvas.drawColor(Color.TRANSPARENT);

        }

        //画出索引字母

        int height = getHeight();

        int width = getWidth();

        int singleHeight = height / b.length;

        for (int i = 0; i < b.length; i++) {

            paint.setTextSize(getResources().getDimension(R.dimen.side_letter_bar_letter_size));

            paint.setColor(getResources().getColor(R.color.main_color));

            paint.setAntiAlias(true);

            //如果选中颜色变深

            if (i == choose) {

                //Toast.makeText(getContext(), "choice", Toast.LENGTH_SHORT).show();

                paint.setColor(getResources().getColor(R.color.gray_deep));

                paint.setFakeBoldText(true);  //加粗

            }

            //计算相应字母的距离居中

            float xPos = width / 2 - paint.measureText(b[i]) / 2;

            float yPos = singleHeight * i + singleHeight;

            canvas.drawText(b[i], xPos, yPos, paint);

            paint.reset();

        }



    }



    //    设置中间显示的结果

    @Override

    public boolean dispatchTouchEvent(MotionEvent event) {

        final int action = event.getAction();

        final float y = event.getY();

        final int oldChoose = choose;

        final OnLetterChangedListener listener = onLetterChangedListener;

        //相应高度的比例乘以字符数组长度就是我们要的字母

        final int c = (int) (y / getHeight() * b.length);



        switch (action) {

            case MotionEvent.ACTION_DOWN:

                showBg = true;

                if (oldChoose != c && listener != null) {

                    if (c >= 0 && c < b.length) {

                        listener.onLetterChanged(b[c]);

                        choose = c;

                        invalidate();

                        if (overlay != null){

                            overlay.setVisibility(VISIBLE);

                            overlay.setText(b[c]);

                        }

                    }

                }



                break;

            case MotionEvent.ACTION_MOVE:

                if (oldChoose != c && listener != null) {

                    if (c >= 0 && c < b.length) {

                        listener.onLetterChanged(b[c]);

                        choose = c;

                        invalidate();

                        if (overlay != null){

                            overlay.setVisibility(VISIBLE);

                            overlay.setText(b[c]);

                        }

                    }

                }

                break;

            case MotionEvent.ACTION_UP:

                showBg = false;

                choose = -1;

                invalidate();

                if (overlay != null){

                    overlay.setVisibility(GONE);

                }

                break;

        }

        return true;

    }



    @Override

    public boolean onTouchEvent(MotionEvent event) {

        return super.onTouchEvent(event);

    }



    public void setOnLetterChangedListener(OnLetterChangedListener onLetterChangedListener) {

        this.onLetterChangedListener = onLetterChangedListener;

    }



    public interface OnLetterChangedListener {

        void onLetterChanged(String letter);

    }
}
